﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace chartsRT.BarChart
{
    public sealed class BarChartUT
    {
        internal class Data
        {
            public string title { get; set; }
            public int value { get; set; }
            public string color { get; set; }
        }

        internal class RootObject
        {
            public string handle { get; set; }
            public string key { get; set; }
            public Data data { get; set; }
        }

        public static string getParams(string DS)
        {
            try
            {
                string pattern = @"""\d"":";
                string JSON = Regex.Replace(string.Format("[{0}]", DS.Substring(1, DS.Length - 2)).ToString(), pattern, string.Empty);
                List<RootObject> dataSource = JsonConvert.DeserializeObject<List<RootObject>>(JSON);
                dynamic Result = new System.Dynamic.ExpandoObject();
               var min= Result.min = dataSource.Min(ee => ee.data.value);
               var max = Result.max = dataSource.Max(ee => ee.data.value);
               var count = Result.count = dataSource.Count();
                var step = (max - min) / count;
                while (step > min)
                {
                    count *= 2;
                    step = (max - min) / count;
                }
                Result.step = step;
                return JsonConvert.SerializeObject(Result);
            }
            catch (Exception) { }
            return "";
        }
    }
}
